﻿using Arction.Wpf.BindableCharting;
using Arction.Wpf.BindableCharting.SeriesPolar;
using Arction.Wpf.BindableCharting.Views.ViewPolar;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WPF_UI_Lightningchart.Events;
using WPF_UI_Lightningchart.ViewModels;

namespace WPF_UI_Lightningchart.UserControls
{
    /// <summary>
    /// NyquistBindableControl.xaml 的交互逻辑
    /// </summary>
    public partial class NyquistBindableControl : UserControl
    {
        private readonly NyquistBindableControlViewModel _viewModel;
        private DispatcherTimer _timerAutoYFit;
        public NyquistBindableControl(IEventAggregator eventAggregator)
        {
            InitializeComponent();
            _viewModel = new NyquistBindableControlViewModel(eventAggregator);
            DataContext = _viewModel;
            this.Loaded += (r, s) =>
            {
                _timerAutoYFit = new DispatcherTimer
                {
                    Interval = new TimeSpan(0, 0, 0, 0, 1000)
                };
                _timerAutoYFit.Tick += new EventHandler(_timerAutoYFit_Tick);
                _timerAutoYFit.IsEnabled = true;
            };
            this.Unloaded += (s, e) =>
            {
                Task.Run(() =>
                {
                    _timerAutoYFit.Tick -= new EventHandler(_timerAutoYFit_Tick);
                    _timerAutoYFit.IsEnabled = false;
                });
            };
        }

        private void _timerAutoYFit_Tick(object sender, EventArgs e)
        {
            if (_viewModel.PointLineSeriesPolarCollection.Count == 0) return;
            double maxAngle = 0; 
            double max = 0;
            double min = 0;
            foreach (var item in _viewModel.PointLineSeriesPolarCollection)
            {
                if (item.GetMinMaxFromAngleRange(out min, out max, 0, 360)) 
                {
                    if(maxAngle <= max) maxAngle = max;
                }
            }
            this.Dispatcher.Invoke(() =>
            {
                axisPolar.MaxAmplitude =  (int)(Math.Ceiling(maxAngle / 5.0) * 5);
                axisPolar.MinAmplitude = 0;
            });
        }

        private void Grid_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = e.Data.GetDataPresent(typeof(SensorViewModel)) ? DragDropEffects.Move : DragDropEffects.None;
            e.Handled = true;
        }
        private void Grid_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(SensorViewModel)))
            {
                var sensorData = e.Data.GetData(typeof(SensorViewModel)) as SensorViewModel;
                _viewModel.OnSeriesAdded(sensorData);
            }
        }
    }
}
